﻿/*
* 创建者：LIYINFAI
* 创建时间：2023/10/15 11:40:00
* 描述：
* --------------------------------------
* 修改说明：
* --------------------------------------
*/
using CommonLibrary.Forms;
using System;
using System.Windows.Forms;

namespace CommonLibrary
{
    /// <summary>
    /// 用户编辑窗体
    /// </summary>
    public partial class FormUserEdit : HWFrmWithTitle
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="user"></param>
        public FormUserEdit(UserInfo user = null)
        {
            InitializeComponent();
            LoadUser(user);
            this.Title = user != null ? "编辑用户" : "新增用户";
        }
        private void LoadUser(UserInfo userInfo = null)
        {
            int powerSelected = 0;
            long id = 0;
            string userNo = string.Empty, userName = string.Empty,
                name = string.Empty,
                password = string.Empty, cardNo = string.Empty;
            if (userInfo != null)
            {
                id = userInfo.Id;
                powerSelected = userInfo.Permission - 1;
                userNo = userInfo.YGBH;
                name = userInfo.Name;
                userName = userInfo.UserName;
                password = EncryptDecryptHelper.Decrypt(userInfo.UserPassword);
                cardNo = EncryptDecryptHelper.Decrypt(userInfo.CreditCardID);
                this.labelID.Tag = userInfo;
            }
            this.labelID.Text = id.ToString();
            this.comboBoxPower.SelectedIndex = powerSelected;
            this.textBoxUserNo.Text = userNo;
            this.textBoxName.Text = name;
            this.textBoxUserName.Text = userName;
            this.textBoxPassword.Text = password;
            this.textBoxCardNo.Text = cardNo;
            if (!string.IsNullOrWhiteSpace(userNo))
            {
                //不可以编辑
                this.textBoxUserNo.ReadOnly = true;
                this.textBoxUserName.ReadOnly = true;
                this.textBoxUserNo.Enabled = false;
                this.textBoxUserName.Enabled = false;
            }
        }
        private void FormUserEdit_Load(object sender, EventArgs e)
        {
            InitPermission();
            this.labelStart.Select();
        }
        private void InitPermission()
        {
            if (UserHelper.LoginUser == null)
            {
                this.comboBoxPower.Items.Clear();
            }
            else
            {
                while (this.comboBoxPower.Items.Count > UserHelper.LoginUser.Permission && this.comboBoxPower.Items.Count > 0)
                {
                    this.comboBoxPower.Items.RemoveAt(this.comboBoxPower.Items.Count - 1);
                }
            }
        }
        private void buttonSave_Click(object sender, EventArgs e)
        {
            int powerSelected = this.comboBoxPower.SelectedIndex;
            long id = long.Parse(this.labelID.Text);
            string userNo = this.textBoxUserNo.Text;
            string name = this.textBoxName.Text;
            string userName = this.textBoxUserName.Text;
            string password = EncryptDecryptHelper.Encrypt(this.textBoxPassword.Text);
            string cardNo = EncryptDecryptHelper.Encrypt(this.textBoxCardNo.Text);
            UserInfo userInfo = this.labelID.Tag == null ? new UserInfo()
            {
                Id = 0,
                Created = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
            } : (UserInfo)this.labelID.Tag;
            userInfo.CreditCardID = cardNo;
            userInfo.Permission = powerSelected + 1;
            userInfo.YGBH = userNo;
            userInfo.Name = name;
            userInfo.UserName = userName;
            userInfo.UserPassword = password;
            if (id == 0)
            {
                //AddUser
                if (UserHelper.AddUser(userInfo))
                {
                    HWMessageBox.Show(this, "新增用户成功");
                    this.Close();
                }
                else
                {
                    HWMessageBox.Show(this, "新增用户失败");
                }
            }
            else
            {
                //EditUser
                if (UserHelper.EditUser(userInfo))
                {
                    HWMessageBox.Show(this, "保存用户成功");
                    this.Close();
                }
                else
                {
                    HWMessageBox.Show(this, "保存用户失败");
                }
            }
        }
        private void buttonMaxSize_Click(object sender, EventArgs e)
        {
            if (this.WindowState == FormWindowState.Maximized)
            {
                this.WindowState = FormWindowState.Normal;
            }
            else
            {
                this.WindowState = FormWindowState.Maximized;
            }
        }
        private void FormUserEdit_Shown(object sender, EventArgs e)
        {
        }
        private void FormUserEdit_FormClosing(object sender, FormClosingEventArgs e)
        {
        }
    }
}
